package com.qxzn.app.personalcenter.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.alibaba.fastjson.JSONObject;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.interfaces.MyInterface;
import com.qxzn.app.page.model.LoginAndRegisterModel;
import com.qxzn.app.personalcenter.R;
import com.qxzn.app.personalcenter.R2;
import com.qxzn.app.util.ActivityStack;
import com.qxzn.app.util.AppUtil;
import com.qxzn.app.util.CommonUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 重置密码
 */
public class ResetPwdActivity extends BaseActivity implements MyInterface.UpdatePwdListener {
    @BindView(R2.id.et_pwd)
    EditText etPwd;
    private String pwd;
    private LoginAndRegisterModel loginAndRegisterModel;

    @Override
    protected int initLayout() {
        return R.layout.activity_reset_pwd;
    }

    @Override
    protected void initView() {
        etPwd.setFilters(new InputFilter[]{new InputFilter.LengthFilter(16), new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                Pattern p = Pattern.compile("([\\ \\\\\\[\\]!\"#$%&'()*+,-./:;<=>?@^_`{|}~]|[0-9a-zA-Z])+");
                Matcher m = p.matcher(source.toString());
                if (!m.matches()) return "";
                return null;
            }
        }});
    }

    @Override
    protected void iniTitleBar() {
        tv_title.setVisibility(View.GONE);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        loginAndRegisterModel = new LoginAndRegisterModel(this);
        loginAndRegisterModel.setUpdatePwdListener(this);
    }

    @OnClick(R2.id.tv_next)
    public void onViewClicked() {
        if (CommonUtil.isFastClick()) {
            return;
        }
        pwd = etPwd.getText().toString().trim();
        if (TextUtils.isEmpty(pwd)) {
            AppUtil.showToast(this, etPwd.getHint().toString());
            return;
        }
        if (!CommonUtil.isLettersAndNumbers(pwd)) {
            AppUtil.showToast(this, "密码不能少于8个字符，至少包含数字和字母");
            return;
        }
        showProgress();
        JSONObject params = new JSONObject();
        params.put("type", 2);
        params.put("username", getIntent().getStringExtra("phone"));
        params.put("old_password", "");
        params.put("new_password", pwd);
        loginAndRegisterModel.updatePwd(params);
    }

    @Override
    public void onUpdatePwdSuccess(BaseBean baseBean) {
        hideProgress();
        if (baseBean != null) {
            if (baseBean.getCode() == 200) {
                showDialog("密码已重置", "您已经成功重置密码，请您使用新密码登录", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        startActivity(new Intent(ResetPwdActivity.this, LoginByPwdActivity.class));
                        ActivityStack.finishAllWithoutActivity(LoginByPwdActivity.class.getName());
                    }
                }).setCancelAble(false).setCanceledOnTouchOutside(false).setNegativeBtnVisible(View.GONE).setDividerVisible(View.GONE);
                return;
            }
        }
        if (baseBean != null && !TextUtils.isEmpty(baseBean.getMsg())) {
            AppUtil.showToast(this, baseBean.getMsg());
        } else {
            AppUtil.showToast(this, "密码重置失败，请重试");
        }
    }

    @Override
    public void onUpdatePwdFailure() {
        hideProgress();
        AppUtil.showToast(this, "网络错误，请稍后再试！");
    }

    @Override
    protected void onDestroy() {
        if (loginAndRegisterModel != null) {
            loginAndRegisterModel.removeListener();
        }
        super.onDestroy();
    }
}
